<?php
namespace app\index\controller;

use app\common\controller\Base;
use think\Db;
use think\cache\driver\Redis;
use GuzzleHttp\json_encode;

class Index extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $start = array(
            'user_all' => $this->user_all,
            'app_down' => $this->app_down,
            'gg_down' => $this->gg_down,
            'logo' => $this->logo
        );
        $this->assign($start);
    }

	   public function home()
    {
	    $iosObj = Db::name('version')->where(['type'=>2,'systemtype'=>2])->order('number DESC')->find();
		$androidObj = Db::name('version')->where(['type'=>2,'systemtype'=>1])->order('number DESC')->find();
		$ios = $android = '';
		if( !empty( $iosObj ) ){
			$ios = 'itms-services://?action=download-manifest&url=https://6709a.com/ios/'.$iosObj['version_url'];
		}          
		if( !empty( $androidObj ) ){
			$android = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/apk/'.$androidObj['version_url'];
		}
		$this->assign('ios',$ios);
		$this->assign('android',$android);
		return $this->fetch('down');
	}
	
    /**
     * @Purpose 电脑版首页入口
     * @Time 2017-11
     */
    public function home1()
    {
    	$redis_time = 300;
    	$redis = new Redis();
        //彩票种类列表
        $lottery_type = $redis->get('home_lottery_type');
        if(!$lottery_type){
        	$lottery_types = $this->lotteryTypeList();
        	$redis->set('home_lottery_type', json_encode($lottery_types, JSON_UNESCAPED_UNICODE),$redis_time);
        	$lottery_type = $redis->get('home_lottery_type');
        }
        //电脑版广告
        $pc_ad = $redis->get('home_pc_ad');
        if(!$pc_ad){
        	$pc_ads = $this->ADForPC();
        	$redis->set('home_pc_ad', json_encode($pc_ads, JSON_UNESCAPED_UNICODE),$redis_time);
        	$pc_ad = $redis->get('home_pc_ad');
        }
        //滚动公告
        $hdlbtest = $redis->get('home_hdlbtest');
        if(!$hdlbtest){
        	$hdlbtests = Db::name('xt_gonggao')->where('xianshi', 1)->order('paixu', 'DESC')->select();
        	$redis->set('home_hdlbtest', json_encode($hdlbtests, JSON_UNESCAPED_UNICODE),$redis_time);
        	$hdlbtest = $redis->get('home_hdlbtest');
        }
        //首页公告列表
        $noner_list = $redis->get('home_noner_list');
        if(!$noner_list){
        	$noner_lists = array_slice($hdlbtest, 0, 4);
        	$redis->set('home_noner_list', json_encode($noner_lists, JSON_UNESCAPED_UNICODE),$redis_time);
        	$noner_list = $redis->get('home_noner_list');
        }
        //首页新闻列表
        $new_list = $redis->get('home_new_list');
        if(!$new_list){
	        $new_lists = db('xt_news')->where('xianshi', 1)->order('paixu', 'DESC')->limit(16)->select();
	        $redis->set('home_new_list', json_encode($new_lists, JSON_UNESCAPED_UNICODE),$redis_time);
	        $new_list = $redis->get('home_new_list');
        }
        //首页中奖排行榜
        $phblists = $redis->get('home_phblists');
        if(!$phblists){
	        $reward_list = db('touzhu_list_ok')->alias('tlo')
	            ->join('__USERNAME__ u', 'u.username=tlo.username')
	            ->field('tlo.username,tlo.zjje')
	            ->where(array('u.swzh'=>0,'u.nbzh'=>0))
	            ->where('tlo.zjje', '>', 0)
	            ->order('tlo.tz_time', 'DESC')
	            ->limit(30)
	            ->select();
	        //拼够一百条
	        $phblist = [];
	        /* if(count($reward_list) < 100) {
	            $count = 100 - count($reward_list);
	            for ($i = 0; $i < $count; $i++) {
	                $usern = suiji_zifu(4);
	                $zhongjje = rand(10000, 1000000);
	                $zhongjje = $zhongjje / 100;
	                $phblist[$i] = [
	                    'usern' => $usern,
	                    'zhongjje' => sprintf('%.2f', $zhongjje),
	                    'id' => $i
	                ];
	            }
	        } */
	        $reward_count = count($phblist);
	        foreach ($reward_list as $k => $v) {
	            ++$reward_count;
	            $phblist[$reward_count] = [
	                'usern' => substr($v['username'], 0, 4),
	                'zhongjje' => sprintf('%.2f', $v['zjje']),
	                'id' => $reward_count
	            ];
	        }
	        //打乱数组
	        //shuffle($phblist);
	        $redis->set('home_phblists', json_encode($phblist, JSON_UNESCAPED_UNICODE),$redis_time);
            $phblists = $redis->get('home_phblists');
        }

        //是否有推荐公告
        $sign = $redis->get('home_sign');
        if(!$sign){
        $signs = Db::name('xt_gonggao')->where('suggest_web', 1)->count();
        $redis->set('home_sign', json_encode($signs, JSON_UNESCAPED_UNICODE),$redis_time);
        $sign = $redis->get('home_sign');
        }

        //查询红包是否开启

        $bonus = Db::name('bonus_lay')->field('flip')->find();


        //首页近期开奖列表
        //【重庆时时彩，安徽快3，山东11选5，PC28】
        //【天津时时彩,排列3,福彩3D,上海时时乐,江西11选5,广东11选5,江苏快3,北京PK拾,幸运飞艇,七星彩,六合彩】
        $cp_list = [8,5,7,48,16,28,44,4,61,51,9];
        $cp = array_rand($cp_list,4);
        $openkjlist_five = [];
        $lottery = Db::name("caipai_list")->column("id,cp_name_new,cp_name_old,initial_class_id");
        foreach($cp as $v){
            $info = $redis->get("result_".$cp_list[$v]);
            $attr = explode(',',trim($info));
            if( !empty($attr) ){
                $openkjlist_five[] = [
                    'czid' => $cp_list[$v],
                    'cp_name_new' => $lottery[$cp_list[$v]]['cp_name_new'],
                    'cp_name_old' => $lottery[$cp_list[$v]]['cp_name_old'],
                    'qihao' => $attr[0],
                    'kjjg' => kjjg_split($attr[2],$cp_list[$v]),
                    'jztime' => date('Y-m-d',$attr[1]),
                    'initial_class_id' => $lottery[$cp_list[$v]]['initial_class_id']
                ];
            }
        }
//        $openkjlist_five = $redis->get('home_openkjlist_five');
//        if(!$openkjlist_five){
//        $openkjlist_fives = \app\index\logic\KjjgList::getRunLotteryRecent();
//        $redis->set('home_openkjlist_five', json_encode($openkjlist_fives, JSON_UNESCAPED_UNICODE),$redis_time);
//        $openkjlist_five = $redis->get('home_openkjlist_five');
//        }


        //重庆时时彩最近开奖结果
        $cq_redis = $redis->get("result_1");
        $cq_redis = explode(',',trim($cq_redis));
        $cq_ssc = [];
        if( !empty($cq_redis) ){
            $cq_ssc['czid'] = 1;
            $cq_ssc['qihao'] = $cq_redis[0];
            $cq_ssc['initial_class_id'] = $lottery[1]['initial_class_id'];
            $cq_ssc = array_merge($cq_ssc, kjjg_split($cq_redis[2],1));
        }

//        $cq_ssc = $redis->get('home_cq_ssc');
//        if($cq_ssc){
//            $cq_sscs = \app\index\model\KjjgList::getRunLotteryResult(1);
//            $cq_sscs = array_merge($cq_sscs, str_split($cq_sscs['kjjg'], 1));
//            $redis->set('home_cq_ssc', json_encode($cq_sscs, JSON_UNESCAPED_UNICODE),$redis_time);
//            $cq_ssc = $redis->get('home_cq_ssc');
//        }
        //安徽快三最近开奖结果
        $k3_redis = $redis->get("result_36");
        $k3_redis = explode(',',trim($k3_redis));
        $ah_fast3 = [];
        if( !empty($k3_redis) ){
            $ah_fast3['czid'] = 36;
            $ah_fast3['qihao'] = $k3_redis[0];
            $ah_fast3['initial_class_id'] = $lottery[36]['initial_class_id'];
            $ah_fast3 = array_merge($ah_fast3, kjjg_split($k3_redis[2],36));
        }
//        $ah_fast3 = $redis->get('home_ah_fast');
//        if(!$ah_fast3){
//        $ah_fast3s = \app\index\model\KjjgList::getRunLotteryResult(36);
//        $ah_fast3s = array_merge($ah_fast3s, str_split($ah_fast3s['kjjg'], 1));
//        $redis->set('home_ah_fast', json_encode($ah_fast3s, JSON_UNESCAPED_UNICODE),$redis_time);
//        $ah_fast3 = $redis->get('home_ah_fast');
//        }
        //山东11选5最近开奖结果
        $sd_redis = $redis->get("result_28");
        $sd_redis = explode(',',trim($sd_redis));
        $sd_11_5 = [];
        if( !empty($sd_redis) ){
            $sd_11_5['czid'] = 28;
            $sd_11_5['qihao'] = $sd_redis[0];
            $sd_11_5['initial_class_id'] = $lottery[28]['initial_class_id'];
            $sd_11_5 = array_merge($sd_11_5, kjjg_split($sd_redis[2],28));
        }
//        $sd_11_5 = $redis->get('home_sd');
//        if(!$sd_11_5){
//        $sd_11_5s = \app\index\model\KjjgList::getRunLotteryResult(28);
//        $sd_11_5s = array_merge($sd_11_5s, str_split($sd_11_5s['kjjg'], 2));
//        $redis->set('home_sd', json_encode($sd_11_5s, JSON_UNESCAPED_UNICODE),$redis_time);
//        $sd_11_5 = $redis->get('home_sd');
//        }
        //Pcdd最近开奖结果
        $dd_redis = $redis->get("result_10");
        $dd_redis = explode(',',trim($dd_redis));
        $pcdd = [];
        if( !empty($dd_redis) ){
            $pcdd['czid'] = 10;
            $pcdd['qihao'] = $dd_redis[0];
            $pcdd['initial_class_id'] = $lottery[10]['initial_class_id'];
            $kjjg = kjjg_split($dd_redis[2],10);
            $pcdd = array_merge($pcdd, $kjjg);
        }
//        $pcdd = $redis->get('home_pcdds');
//        if(!$pcdd){
//            $he=array('he'=>'');
//            $pcdds = \app\index\model\KjjgList::getRunLotteryResult(10);
//            $he['he'] = substr($pcdds['kjjg'], 4);
//            $pcdds = array_merge($pcdds, str_split($pcdds['kjjg'], 1));
//            $pcdds = array_merge($pcdds,$he);
//            $redis->set('home_pcdds', json_encode($pcdds, JSON_UNESCAPED_UNICODE),$redis_time);
//            $pcdd = $redis->get('home_pcdds');
//        }

        //彩种总数
//        $czsl = Db::name('caipai_list')->where('cp_if_open', 1)->count();
        //客服QQ
        $zxkf = $redis->get('home_zxkf');
        if(!$zxkf){
        $zxkfs = Db::name('xt_kflink')->where('type', 4)->field('qq')->find();
        $redis->set('home_zxkf', json_encode($zxkfs, JSON_UNESCAPED_UNICODE),$redis_time);
        $zxkf = $redis->get('home_zxkf');
        }
		
        $list = $redis->get('sign');
        if (!$list) {
            $lists = Db::name('xt_gonggao')->where('suggest_web', 1)->field('id,title,ggtext')->order('fatime desc')->select();
            $redis->set('sign', json_encode($lists, JSON_UNESCAPED_UNICODE),300);
            $list = $redis->get('sign');
        }
        $this->assign('list', $list);

        $this->assign('zxkf', $zxkf['qq']);
        $this->assign('sign', $sign);
        $this->assign($lottery_type);
        $this->assign($pc_ad);
        $this->assign('bonus', $bonus);
        $this->assign('lblist', $hdlbtest);
        $this->assign('gglist', $noner_list);
        $this->assign('xinwlist', $new_list);
        $this->assign('phblist', $phblists);
        $this->assign('uudisd', $openkjlist_five);
        $this->assign('openno_1', $cq_ssc);
        $this->assign('openno_2', $ah_fast3);
        $this->assign('openno_3', $sd_11_5);
        $this->assign('openno_4', $pcdd);
        $this->assign('cur_time', time());
        return $this->fetch('index');
    }

    /**
     * @Purpose 电脑版注册
     * @Time 2017-11-16
     */
    public function register()
    {
        //彩票种类列表
        $lottery_type = $this->lotteryTypeList();
        //电脑版广告
        $pc_ad = $this->ADForPC();
        //获取验证码开关
        $codeif = Db::name('xt_reg')->find();
        $this->assign('tjm', cookie('vip'));
        $this->assign($lottery_type);
        $this->assign($pc_ad);
        $this->assign('code_open', $codeif['codo_open']);
        $this->assign('tjm_open', $codeif['tjr_open']);
        return $this->fetch();
    }

    /**
     * @Purpose 电脑版注册协议
     * @Time 2017-12-27
     */
    public function regAgreement()
    {
        return $this->fetch('reg_agreement');
    }

    /**
     * @Purpose 电脑版试玩注册
     * @Time 2018-01-03
     */
    public function tryPlay()
    {
        //彩票种类列表
        $lottery_type = $this->lotteryTypeList();
        //电脑版广告
        $pc_ad = $this->ADForPC();
        //获取验证码开关
        $codeif = Db::name('xt_reg')->find();

        $this->assign($lottery_type);
        $this->assign($pc_ad);
        $this->assign('code_open', $codeif['codo_open']);
        return $this->fetch('try_play');
    }

    /**
     * @Purpose 平台公告
     * @Time 2018-05-30
     */
    public function sign()
    {
//      $redis = new Redis();
//      $list = $redis->get('sign');
//      if (!$list) {
            $lists = Db::name('xt_gonggao')->where('suggest_web', 1)->field('id,title,ggtext')->order('fatime desc')->select();
//          $redis->set('sign', json_encode($lists, JSON_UNESCAPED_UNICODE),300);
//          $list = $redis->get('sign');
//      }
        $this->assign('list', $lists);
        return $this->fetch();
    }
}
